package cn.itcast.web;

import cn.itcast.service.UserService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "Serch")
public class SerchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取前端关键字  keyword
        String keyword = request.getParameter("keyword");
        // 找service 完成模糊查询
        UserService service = new UserService();
        List<String> list = service.search(keyword);
        //list---json
        String result = JSON.toJSONString(list);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(result);
    }
}
